package com.hdu.other.nestedRecursion;


public class lc394 {
    class Solution {
        int where;
        public String decodeString(String s)
        {
              int len = s.length();
              return f(s.toCharArray(), 0);
        }

        String f(char[] s, int i)
        {
            int len = s.length;
            int cur = 0;
            StringBuilder ans = new StringBuilder();
            while (i < len && s[i] != ']')
            {
                if (Character.isDigit(s[i]))
                {
                    cur = cur * 10 + s[i++] - '0';
                }
                else if (s[i] == '[')
                {
                    ans.append(get(cur, f(s, i + 1)));
                    i = where + 1;
                    cur = 0;
                }
                else
                {
                    ans.append(s[i++]);
                }
            }
            where = i;
            return ans.toString();
        }

        String get(int cnt, String s)
        {
            StringBuilder ans = new StringBuilder();
            while (cnt-- > 0)
            {
                ans.append(s);
            }
            return ans.toString();
        }

    }
}
